package com.chinawayltd.ews.sample.service.algorithm.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import com.chinawayltd.ews.sample.mapper.TDriverLoginMapper;
import com.chinawayltd.ews.sample.mapper.algorithm.DriverLoginExtendMapper;
import com.chinawayltd.ews.sample.model.TDriverLogin;
import com.chinawayltd.ews.sample.service.algorithm.DriverLoginService;
import com.chinawayltd.ews.sample.util.StringUtils;
import com.chinawayltd.ews.sample.web.query.DriverLoginGirdQuery;

/**
 * @program: greeProject
 * @description 实现类
 * @author: chenxin
 * @create: 2018年4月11日 下午10:52:48
 */
@Service
public class DriverLoginServiceImpl implements DriverLoginService{

    @Autowired
    TDriverLoginMapper tDriverLoginMapper;
    @Autowired
    DriverLoginExtendMapper driverLoginExtendMapper;

	@Override
	public List<TDriverLogin> list(DriverLoginGirdQuery queryParam) {
		 return driverLoginExtendMapper.list(queryParam);
	}
	
	@Override
	public TDriverLogin getById(Long id) throws Exception{
		if(id == null || id <= 0){
            throw new RuntimeException("id不能为空或者少于等于0！");
        }
		return tDriverLoginMapper.selectByPrimaryKey(id);
	}

	@Override
	@Transactional
	public int add(TDriverLogin tDriverLogin) {
		tDriverLogin.setCreateTime(new Date());
		tDriverLogin.setUpdateTime(new Date());
		// 密码要进行md5加密
		if(StringUtils.isNoneBlank(tDriverLogin.getPassword())){
			tDriverLogin.setPassword(DigestUtils.md5DigestAsHex(tDriverLogin.getPassword().getBytes()));
		}
		return tDriverLoginMapper.insertSelective(tDriverLogin);
	}
	
	@Override
	@Transactional
	public int update(TDriverLogin tDriverLogin) {
		tDriverLogin.setUpdateTime(new Date());
		// 密码要进行md5加密
		if(StringUtils.isNoneBlank(tDriverLogin.getPassword())){
			tDriverLogin.setPassword(DigestUtils.md5DigestAsHex(tDriverLogin.getPassword().getBytes()));
		}
		return tDriverLoginMapper.updateByPrimaryKeySelective(tDriverLogin);
	}

	@Override
	public int delete(Long id) throws Exception{
		if(id == null || id <= 0){
            throw new RuntimeException("id不能为空或者少于等于0！");
        }
		return tDriverLoginMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<TDriverLogin> getDriverLoginByNameAndId(Long driverId, String loginName) {
		Map<String, Object> map = new HashMap<>();
		map.put("driverId", driverId);
		map.put("loginName", loginName);
		return driverLoginExtendMapper.getDriverLoginByNameAndId(map);
	}

 
}
